package com.zimi.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zimi.model.SysOrgExtend;
import com.zimi.model.SysPost;
import com.zimi.service.SysPostService;
import com.zimi.util.GsonUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 机构职务表 api
 *
 * @author lnintr
 * @since 2019-05-22
 */
@RestController
@RequestMapping("sys-post")
public class SysPostApi {
    @Autowired
    private SysPostService sysPostService;

    @PostMapping("postList")
    @ApiOperation("查询机构信息")
    public Object selectPostList(HttpServletRequest request, int limit, int page) {
        String[] ma = request.getParameterValues("orgIds");

        List<Integer> orgIds = GsonUtil.json2Objs(ma[0], Integer.class);

        Map<String, Object> objectMap = new HashMap<>();
        PageHelper.startPage(page, limit);
        List<SysOrgExtend> list = sysPostService.getPostlistByOrg(orgIds);
        PageInfo<SysOrgExtend> pageInfo = new PageInfo<>(list);
        objectMap.put("data", list);
        objectMap.put("count", pageInfo.getTotal());
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("savePost")
    @ApiOperation("修改机构和职务信息")
    public Object savePost(SysOrgExtend record) {
        sysPostService.savePost(record, 1);

        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("deletePost")
    @ApiOperation("删除机构和职务信息")
    public Object deletePost(SysOrgExtend record) {
        sysPostService.savePost(record, 2);

        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("code", 0);
        return objectMap;
    }


    @PostMapping("{id}")
    @ApiOperation("根据Id查询机构")
    public Object getPostById(@PathVariable("id") int id) {
        Map map = new HashMap();
        map.put("code", 0);
        map.put("sysPost", sysPostService.selectById(id));
        return map;
    }

    @PostMapping("update")
    @ApiOperation("根据Id查询信息")
    public Object update(SysPost sysPost) {
        Map map = new HashMap();
        sysPostService.update(sysPost);

        map.put("code", 0);
        return map;
    }
}
